package com.IT.dao.Impl;

import com.IT.dao.AdminFunctionDao;
import com.IT.entity.Log;
import com.IT.entity.Medicine;
import com.IT.entity.User;
import com.IT.utils.DBUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class AdminFunctionDaoImpl implements AdminFunctionDao {
    @Override
    public int addMedicine(String mName, String createDate, String updateDate) {
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;

        try {
            conn = DBUtils.getConnection();
            statement = conn.createStatement();
            rs = statement.executeQuery("select id from t_medicine where mName = '"+mName+"'");
            if(rs.next()){
                //药品已存在
                return -1;
            }else {
                //添加成功
                statement.executeUpdate("insert into t_medicine (mName, createDate, updateDate) values ('" + mName + "', '" + createDate + "', '" + updateDate + "')");
                return 1;
            }
        }catch (Exception e){
            e.printStackTrace();
            //数据库操作异常
            return 0;
        }finally {
            DBUtils.closeAll(conn, statement, rs);
        }
    }

    @Override
    public int deleteMedicine(String mName) {
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;

        try {
            conn = DBUtils.getConnection();
            statement = conn.createStatement();
            rs = statement.executeQuery("select mName from t_medicine where mName = '"+mName+"'");
            if(rs.next()) {
                //删除成功！
                statement.executeUpdate("delete from t_medicine where mName = '" + mName + "'");
                return 1;
            }else {
                //该药品不存在
                return -1;
            }
        }catch (Exception e){
            e.printStackTrace();
            //数据库操作异常
            return 0;
        }finally {
            DBUtils.closeAll(conn, statement, rs);
        }
    }

    @Override
    public int updateMedicine(String mName, String nMName, String updateDate) {
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;

        try {
            conn = DBUtils.getConnection();
            statement = conn.createStatement();
            rs = statement.executeQuery("select id from t_medicine where mName = '"+mName+"'");
            if(rs.next()) {
                //修改成功！
                statement.executeUpdate("update t_medicine set mName = '" + nMName + "' where mName = '" + mName + "'");
                statement.executeUpdate("update t_medicine set updateDate = '" + updateDate + "' where mName = '" + mName + "'");
                return 1;
            }else {
                //该药品不存在！
                return -1;
            }
        }catch (Exception e){
            e.printStackTrace();
            //数据库操作异常！
            return 0;
        }finally {
            DBUtils.closeAll(conn, statement, rs);
        }
    }

    @Override
    public List<Medicine> queryMedicine() {
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;

        int id = 1;
        List<Medicine> medicineList = new ArrayList<>();
        try {
            conn = DBUtils.getConnection();
            statement = conn.createStatement();
            rs = statement.executeQuery("select * from t_medicine");
            while (rs.next()){
                String mName = rs.getString("mName");
                String createDate = rs.getString("createDate");
                String updateDate = rs.getString("updateDate");

                Medicine medicine = new Medicine(id, mName, createDate, updateDate);
                medicineList.add(medicine);
                id++;
            }
            return medicineList;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            DBUtils.closeAll(conn, statement, rs);
        }
    }

    @Override
    public List<Log> queryLog() {
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;

        List<Log> logList = new ArrayList<>();
        try {
            conn = DBUtils.getConnection();
            statement = conn.createStatement();
            rs = statement.executeQuery("select * from t_log");
            while (rs.next()){
                logList.add(new Log(Integer.parseInt(rs.getString("id")), Long.parseLong(rs.getString("account")), rs.getString("realName"), Integer.parseInt(rs.getString("authority")), rs.getString("content"), rs.getString("ip"), rs.getString("loginTime")));
            }
            return logList;     //查询成功
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtils.closeAll(conn, statement, rs);
        }
        return null;    //数据库操作异常
    }
}
